package org.judking.carkeeper.src.controller;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Modifier;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.ArrayUtils;
import org.judking.carkeeper.src.DAO.IPddDAO;
import org.judking.carkeeper.src.bean.CommandTransmitter;
import org.judking.carkeeper.src.model.PddDataModel;
import org.judking.carkeeper.src.model.RouteModel;
import org.judking.carkeeper.src.model.UserModel;
import org.judking.carkeeper.src.service.ResourceService;
import org.judking.carkeeper.src.service.UserService;
import org.judking.carkeeper.src.util.DbHelper;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.util.UrlPathHelper;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	@Qualifier("userService")
	UserService userService;
	
	@RequestMapping(method=RequestMethod.POST, value="/register/")
	public String register(
			@RequestParam String username,
			@RequestParam String passwd,
			ModelMap modelMap) 	{

		UserModel userModel = userService.registry(username, passwd);
		String privateToken = userModel.getPrivate_token();

		modelMap.addAttribute("state", "privateToken=\""+privateToken+"\"");
		return "org/judking/carkeeper/resource/page/state";
	}
	
	@RequestMapping(method=RequestMethod.POST, value="/login/")
	public String login(
			@RequestParam String username,
			@RequestParam String passwd,
			ModelMap modelMap) 	{

		String privateToken = userService.login(username, passwd);

		modelMap.addAttribute("state", "privateToken=\""+privateToken+"\"");
		return "org/judking/carkeeper/resource/page/state";
	}
	
	
}
